package cn.com.nou.base.controller;

import cn.com.nou.base.service.bdm.ResourceClassificationService;
import cn.com.nou.common.BaseResponse;
import cn.com.nou.common.builder.PaginationBuilder;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resourceclassification")
public class ResourceClassificationController {
    @Autowired
    ResourceClassificationService classificationService;

    @PostMapping("/add")
    public ResponseEntity<BaseResponse> addResourceClassification(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        nouEnumerationT.setId(Utils.getUUID());
        try {
            boolean flag = classificationService.getResourceClassificationListCount(nouEnumerationT.getItemCode(), nouEnumerationT.getItemName());
            if (flag) {
                classificationService.addResourceClassification(nouEnumerationT);
                return BaseResponse.generateOKResponseEntity("添加成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "添加失败，重复资源代码", "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(501, "系统错误", "");
        }
    }

    @DeleteMapping("/delete")
    public ResponseEntity<BaseResponse> deleteResourceClassification(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        try {


            String id = nouEnumerationT.getId();
            boolean flag = classificationService.getUsedResourceClassificationCount(id);
            System.out.println(flag + "---------------------");
            if (flag) {
                classificationService.deleteResourceClassification(id);
                return BaseResponse.generateOKResponseEntity("删除成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "删除失败，该资源已经被禁用", "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(501, "系统错误", "");
        }
    }

    /**
     * 根据id更新
     *
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    public ResponseEntity<BaseResponse> updateResourceClassification(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        try {
            String id = nouEnumerationT.getId();
            boolean flag = classificationService.getUsedResourceClassificationCount(id);
            if (flag) {
                System.out.println(nouEnumerationT.getItemCode());
                System.out.println(nouEnumerationT.getItemName());
                boolean flagCode = classificationService.getResourceClassificationListCount(nouEnumerationT.getItemCode(), nouEnumerationT.getItemName());
                if (flagCode) {
                    classificationService.updateResourceClassification(nouEnumerationT);
                    return BaseResponse.generateOKResponseEntity("编辑成功", "");
                } else {
                    return BaseResponse.generateBadResponseEntity(500, "存在相同资源分类代码或资源分类名称，不可编辑", "");
                }
            } else {
                return BaseResponse.generateBadResponseEntity(500, "该id不存在", "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(501, "系统错误", "");
        }
    }
    @PostMapping("/list")
    public ResponseEntity<BaseResponse> getResourceClassificationList(@RequestBody NouEnumerationT nouEnumerationT)throws Exception{
        try {
            return BaseResponse.generateOKResponseEntity("查询成功",
                    classificationService.getResourceClassificationList(nouEnumerationT,nouEnumerationT.getPageNo(),nouEnumerationT.getPageSize()));
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(501, "系统错误", "");
        }
    }
}
